﻿Imports System.IO
Imports System.Data

Public Class frmEntryChoixProtocol
    Public nb As Integer
    Private m_ProtocolName As String
    Private m_protId As String
    Private m_protNbFields As Integer
    Private m_selectedProtocolId As String
    Private m_multiSpecies As Boolean
    Private m_mixedSiteType As Boolean
    Public olistProtocoles As clsProtocolesList
    Public bTypeSite, DateNom As String
    Public previousStep As String

    Private Sub frmEntryChoixProtocol_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.GotFocus
        previous = Me.Name
        If olistProtocoles Is Nothing Then
            olistProtocoles = New clsProtocolesList(FormMain.NSDossier & "XML_ProtocolDef.xml")
        End If
    End Sub

    Private Sub frmEntryChoixProtocol_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim str As String = ""
        Dim oProtocol As clsProtocol
        Dim nbProtocols As Short
        Dim yLoc As Integer = 15
        Dim RdBtn As RadioButton

        DateNom = Now.ToString("yyMMdd")
        olistProtocoles = New clsProtocolesList(FormMain.NSDossier & "XML_ProtocolDef.xml")
        ' Exemple 1 ajouter les protocoles à la listBox
        nbProtocols = olistProtocoles.Protocols.Count
        Dim intCtPr As Integer = 0
        For Each de As DictionaryEntry In olistProtocoles.Protocols
            oProtocol = DirectCast(de.Value, clsProtocol)
            m_ProtocolName = oProtocol.getName
            m_protId = oProtocol.protocolDisplayName
            '  recuperer le nombre de champs par protocole
            m_protNbFields = CType(oProtocol.fieldsInfos.Item(0), clsFieldsInfo).fieldsNumber
            ' creer 1 controle par protocole
            RdBtn = New RadioButton
            RdBtn.Name = "rd_" & m_protId
            RdBtn.Text = m_ProtocolName
            RdBtn.Font = New Font("Tahoma", 11, FontStyle.Regular)
            RdBtn.Location = New Point(20, yLoc)
            RdBtn.Size = New Size(240, 20)
            RdBtn.Anchor = AnchorStyles.Top
            If intCtPr = 0 Then
                RdBtn.Checked = True
                m_selectedProtocolId = m_protId
            End If
            AddHandler RdBtn.Click, AddressOf rb_CheckedChanged
            yLoc += 35
            intCtPr = intCtPr + 1
            Me.panel_PrListe.Controls.Add(RdBtn)
        Next de
        ' creation d'une liste contenant toutes les obs des protocoles
        If IsNothing(oProtocolsCurrentListObs) Then
            oProtocolsCurrentListObs = New Hashtable
        End If
        ' savoir si le protocole selectionné concerne plusieures especes  
        m_multiSpecies = DirectCast(olistProtocoles.Protocols(m_selectedProtocolId), clsProtocol).multiSpecies
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmEntryProtocol.Show()
        Me.Hide()
    End Sub

    Public Property ProtocolName() As String
        Get
            Return m_ProtocolName
        End Get
        Set(ByVal value As String)
            m_ProtocolName = value
        End Set
    End Property

    Public Property ProtocolId() As Integer
        Get
            Return m_protId
        End Get
        Set(ByVal value As Integer)
            m_protId = value
        End Set
    End Property

    Public Property ProtocolNbFields() As Integer
        Get
            Return m_protNbFields
        End Get
        Set(ByVal value As Integer)
            m_protNbFields = value
        End Set
    End Property

    Public Property selectedProtocolId() As String
        Get
            Return m_selectedProtocolId
        End Get
        Set(ByVal value As String)
            m_selectedProtocolId = value
        End Set
    End Property

    Public Property multiSpecies() As Boolean
        Get
            Return m_multiSpecies
        End Get
        Set(ByVal value As Boolean)
            value = m_multiSpecies
        End Set
    End Property

    Public ReadOnly Property mixedSiteType() As Boolean
        Get
            Return m_mixedSiteType
        End Get
    End Property

    Private Sub CtlLightGPSTrack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmUtilityTracks.Show()
        Me.Hide()
    End Sub

    Private Sub btnDataShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDataShow.Click
        If m_selectedProtocolId = "Photo" Then
            Try
                frmDataChoix.CreatePictureTable()
                frmDataPictureTable.Show()
                Me.Hide()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Pocket eReleve")
            End Try
        Else
            Dim succeed As Boolean = frmDataObsTable.initDataGrid(oProtocolsCurrentListObs, selectedProtocolId)
            If succeed Then
                frmDataObsTable.Show()
                Me.Hide()
            End If
        End If
    End Sub

    Private Sub btnSuivant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuivant.Click
        Try
            ' si le form actuel est afficher pour selectionner le protocole pour afficher la table de données sauvegardés
            If previousStep = "Data" Then
                ' s'il s'agit du prot photo georéferencée afficher la table correspondante
                ' sinon afficher la table de données relative au protocole sélectionné
                previous = Me.Name
                If m_selectedProtocolId = "Photo" Then
                    Try
                        frmDataChoix.CreatePictureTable()
                        frmDataPictureTable.Show()
                        Me.Hide()
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Critical, "Pocket eReleve")
                    End Try
                    Exit Sub
                End If

                Dim succeed As Boolean = frmDataObsTable.initDataGrid(oProtocolsCurrentListObs, Me.selectedProtocolId)
                If succeed Then
                    ' frmDataObsTable.previous = Me.Name
                    frmDataObsTable.Show()
                    Me.Hide()
                End If
            ElseIf previousStep = "Entry" Then
                'Dim suivi As Boolean
                'Intialisation des données saisie pécédement
                frmEntryProtocol.maIndexStation = Nothing
                frmEntryProtocol.monIdZoneEtude = Nothing
                frmEntryProtocol.maZoneEtude = Nothing
                frmEntryProtocol.oProtocoleValues = Nothing
                m_mixedSiteType = DirectCast(olistProtocoles.Protocols(selectedProtocolId), clsProtocol).monitoring

                ' si protocole multi-especes, charger le fichier taxa
                If multiSpecies = True Then
                    ' recuperer le mon de fichier taxa et le charger 
                    Dim taxaFileName As String = DirectCast(olistProtocoles.Protocols(selectedProtocolId), clsProtocol).TaxonomyFile
                    frmEntryChoixTaxon.initTaxonListe(taxaFileName)
                End If

                If m_mixedSiteType = True Then
                    frmEntryChoixTypeObs.Show()
                    Me.Hide()
                Else
                    frmEntryGPS.Show()
                    Me.Hide()
                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Pocket eRelevé")
            Exit Sub
        End Try
    End Sub

    Private Sub btnMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMenu.Click
        frmEntryFin.dEntryStep = 0
        FormMain.Show()
        Me.Hide()
    End Sub

    Private Sub rb_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim str_idSelectedProtocol As String

        'Dim  ctl As RadioButton In Me.Controls
        'If CType(ctl, Object) = sender Then
        str_idSelectedProtocol = CType(sender, RadioButton).Name
        ' str_idSelectedProtocol = ctl.Name
        m_selectedProtocolId = str_idSelectedProtocol.Substring(3, str_idSelectedProtocol.Length - 3)
        m_multiSpecies = DirectCast(olistProtocoles.Protocols(m_selectedProtocolId), clsProtocol).multiSpecies
        '  End If
        '  Next
    End Sub

    Private Sub frmEntryChoixProtocol_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = System.Windows.Forms.Keys.Up) Then
            'Up
        End If
        If (e.KeyCode = System.Windows.Forms.Keys.Down) Then
            'Down
        End If
        If (e.KeyCode = System.Windows.Forms.Keys.Left) Then
            'Left
        End If
        If (e.KeyCode = System.Windows.Forms.Keys.Right) Then
            'Right
        End If
        If (e.KeyCode = System.Windows.Forms.Keys.Enter) Then
            'Enter
        End If

    End Sub

End Class
